/*
 * To change this template, choose Tools | Templates
 * AND open the template in the editor.
 */

package modelos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author usuario
 */
public class ModeloIngrediente extends ConexionDB{

    private String descripcion,id,estatus;
    private float stock;

    public ModeloIngrediente() {
        super();
    }

    public boolean registar(){
        conectar();
        boolean sw = false;
        String tira="INSERT INTO Ingrediente (id, descripcion, stock , estatus) VALUES('"+getID()+"','"+getDescripcion()+"',"+getStock()+",'A')";
        System.out.println(tira);
        try {
            Statement stam = conexion.createStatement();
            stam.execute(tira);
            sw= true;
        } catch (SQLException ex) {
                   Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     
             sw= false;
        }
        
        desconectar();
        return sw;
    }

  public boolean actualizar(){
     conectar();
     boolean sw = false;
     String tira="UPDATE Ingrediente SET stock ="+getStock()+" WHERE id='"+getID()+"'  AND estatus = 'A'";
     try {
          Statement stam = conexion.createStatement();
            stam.execute(tira);
            sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     desconectar();
     return sw;
 }

  public boolean  consultar(){
    conectar();
    boolean sw = false;
    String tira="SELECT * FROM Ingrediente WHERE id='"+getID()+"' AND estatus='A'";
    try {
         Statement stam = conexion.createStatement();
            ResultSet rs =  stam.executeQuery(tira);
        if (rs.next()){
            sw = true;
            setDescripcion(rs.getString("descripcion"));
            setID(rs.getString("id"));
            setStatus(rs.getString("estatus"));
            setStock(rs.getFloat("stock"));
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sw;
  }

  public LinkedList<ModeloIngrediente> listar(){
    LinkedList<ModeloIngrediente> v = new LinkedList<ModeloIngrediente>();
    String tira="SELECT * FROM Ingrediente WHERE estatus='A'";
    conectar();
    try {
        Statement stam = conexion.createStatement();
        ResultSet rs =  stam.executeQuery(tira);
        while (rs.next()){
            ModeloIngrediente  i = new ModeloIngrediente();
            i.setDescripcion(rs.getString("descripcion"));
            i.setID(rs.getString("id"));
            i.setStatus(rs.getString("estatus"));
            i.setStock(rs.getFloat("stock"));
            v.add(i);
         }
    } catch (SQLException ex) {
        Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    desconectar();
    return v;
  }


  
  public boolean revisarStock(float porcion){
      consultar();
      if((getStock()-porcion)>=0)
          return true;
      else
          return false;
  }

  public void disminuirStock(){
       conectar();
       String tira="UPDATE Ingrediente SET stock ="+getStock()+" WHERE id='"+getID()+"'  AND estatus = 'A'";

       try {
           Statement stam = conexion.createStatement();
           stam.execute(tira);
       } catch (SQLException ex) {
           Logger.getLogger(ModeloIngrediente.class.getName()).log(Level.SEVERE, null, ex);
       }
       desconectar();
  }
  
  public String getDescripcion() {
        return descripcion;
  }

  public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
  }

    public String getID() {
        return id;
    }

    public void setID(String id) {
        this.id = id;
    }

    public float getStock() {
        return stock;
    }

    public void setStock(float stock) {
        this.stock = stock;
    }

    public String getStatus() {
        return estatus;
    }

    public void setStatus(String estatus) {
        this.estatus = estatus;
    }


}
